﻿package smooth.core.object.youTubeComponent {
	import flash.display.Sprite;
	import flash.media.Video;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	import flash.net.URLVariables;
	import flash.events.Event;	
	import flash.events.MouseEvent;
	import smooth.core.canvas.multimediaCanvas.MultimediaCanvas;
	import smooth.core.object.youTubeVideoComponent.YouTubeVideoComponent;
	import smooth.core.object.youTubeVideoComponent.YouTubeVideoPlayer;
	
	import app.core.object.VideoObject;
	
	public class YouTubeLoader extends Sprite {
		
		private var index:int = 0;		
		private var rest:URLLoader = null;
		private var youTube:XML = null;
		private var youTubeComponent:YouTubeComponent;
		private var fetchCount:Number;
		private var allVids:Array;	
		private var requestLabel:String;
		
		public var video:YouTubeVideoComponent;
		//public var video:YouTubeVideoPlayer;
		
		private static var media : Namespace;
		private static var openSearch : Namespace;
		private static var yt : Namespace;
		private static var gd : Namespace;

		private static var YOUTUBE_URL_API:String = "http://gdata.youtube.com/feeds/api/videos";
		private static var YOUTUBE_KEY_API:String = "AI39si4xxHloimXnKNn4WpHzm1xqHS6cxfsXsAKiNHEASNxbb_qRLPDQU3VRW-5ym40LYyNCZSEtuC1JQGL6LkHxbH6hwlGTeg";
		private static var YOUTUBE_METHOD_SEARCH:String = "vq";
		private static const YOUTUBE_TAG_GD:String = "gd";
		private static const YOUTUBE_TAG_YT:String = "yt";
		private static const YOUTUBE_TAG_MEDIA:String = "media";
		private static const YOUTUBE_TAG_OPEN_SEARCH:String = "openSearch";
		
		public function YouTubeLoader(d:YouTubeComponent, fetchIn:Number) {
			youTubeComponent = d;
			fetchCount = fetchIn;
			allVids = new Array();
		}
		
		public function searchText(searchText:String = ""):void {	
			trace("YouTubeLoader::searchText::debut");
			doClearVids();
			this.requestLabel = searchText;
			var request:URLRequest = new URLRequest(YOUTUBE_URL_API);
			var variables:URLVariables = new URLVariables();
			variables.vq = searchText;
			variables.client = YOUTUBE_KEY_API;
			request.data = variables;
			rest = new URLLoader();
			rest.addEventListener("complete", doParse , false, 0, true);
			youTubeComponent.doAnimationFadeOut();
			rest.load(request);
			trace("YouTubeLoader::searchText::fin");
		}
		
		// START - 20081121 - LSA
		public function searchGeoLocal(lat:Number, lng:Number, distance:Number = 5):void {
			trace("YouTubeLoader::searchGeoLocal::debut");
			doClearVids();
			this.requestLabel = "Lat:" + lat.toFixed(3) + " / Lng:" + lng.toFixed(3);
			var request:URLRequest = new URLRequest(YOUTUBE_URL_API);
			var variables:URLVariables = new URLVariables("location="+lat+","+lng+"!&location-radius="+distance+"km");
			variables.client = YOUTUBE_KEY_API;
			request.data = variables;
			rest = new URLLoader();
			rest.addEventListener("complete", doParse , false, 0, true);
			youTubeComponent.doAnimationFadeOut();
			rest.load(request);
			trace("YouTubeLoader::searchGeoLocal::fin");
		}
		// END - 20081121 - LSA
		
		private function doParse(event:Event):void {			
			youTube = new XML(rest.data);
			doDisplayVideos();
		}
		
		private function doDisplayVideos():void {
			trace("YouTubeLoader::doDisplayVideos::debut");
			var id:String = null;
			var title:String = null;
			var selfUrl:String = null;			
			var request:URLRequest = null;			
			var youTubeXmlElements:XMLList = null;
			
			// Initialisation des namespaces
			yt = youTube.namespace(YOUTUBE_TAG_YT);
			media = youTube.namespace(YOUTUBE_TAG_MEDIA);
			openSearch = youTube.namespace(YOUTUBE_TAG_OPEN_SEARCH);
			gd = youTube.namespace(YOUTUBE_TAG_GD);
			
			var len:int = youTube.openSearch::totalResults;
			if (len > 0) {
				youTubeXmlElements = youTube..media::group;
				
				var size:int = 600;
				var posX:int = (this.youTubeComponent.stage.stageWidth-size)/2;
				var posY:int = (this.youTubeComponent.stage.stageHeight-size)/2;
				var canvas1:MultimediaCanvas = new MultimediaCanvas(this.requestLabel, posX, posY, size, size, 0x0000FF);
				youTubeComponent.parent.addChild(canvas1);
				canvas1.doAnimationFadeIn();
				
				if(len > fetchCount)
					len = fetchCount;
				
				for (var i:int = 0; i < len; i++) {
					title = youTubeXmlElements[i].media::title;
					selfUrl = youTubeXmlElements[i].media::player.@url;
					id = selfUrl.substring(selfUrl.lastIndexOf("watch?v=") + 8, selfUrl.length);
					
					var videoObject:YouTubeVideoObject = new YouTubeVideoObject(id, title, selfUrl);
					video = new YouTubeVideoComponent(YOUTUBE_KEY_API, "video_"+i, videoObject, canvas1.width/2, canvas1.height/2, 0.5);
					canvas1.pushMultimediaObject(video);
					allVids.push(video);
					video.doAnimationFadeIn(canvas1.width, canvas1.height);
				}
				youTubeComponent.doAnimationFadeIn();
				youTubeComponent.doInitSearchLabel("");
			} else {
				trace("No video returned by YouTube");
				youTubeComponent.doAnimationFadeIn();
				youTubeComponent.doInitSearchLabel("No video found");
			}
			trace("YouTubeLoader::doDisplayVideos::fin");
		}
		
		private function doClearVids():void {
			allVids = new Array();
		}
	}
	
}